﻿//-----------------------------------------------------------------------
// <copyright file="UrlRetriever.cs" company="billfellows.net">
//     I mention copyright so StyleCop is happy.
// </copyright>
//-----------------------------------------------------------------------
namespace ReputationMinder
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;

    /// <summary>
    /// This class handles calling out to web pages and hangs onto stuff.
    /// </summary>
    public class UrlRetriever
    {
        /// <summary>
        /// An instance of the request object
        /// </summary>
        private HttpWebRequest request;

        /// <summary>
        /// An instance of the response object
        /// </summary>
        private HttpWebResponse response;

        /// <summary>
        /// Retrieves the status code for a particular web call
        /// </summary>
        /// <param name="url">The URL to check</param>
        /// <returns>An instance of HttpStatusCode</returns>
        public HttpStatusCode CheckEm(string url)
        {
            // http://stackoverflow.com/questions/1330856/getting-http-status-code-number-200-301-404-etc-from-httpwebrequest-and-ht
            this.request = (HttpWebRequest)WebRequest.Create(url);
            ((HttpWebRequest)this.request).UserAgent = "Request Test";
            this.request.Method = "GET";

            using (this.response = (HttpWebResponse)this.request.GetResponse())
            {
                return this.response.StatusCode;
            }
        }

        /// <summary>
        /// Download url contents
        /// </summary>
        /// <param name="url">The url to be visited</param>
        /// <returns>The content and some metadata about the visit</returns>
        public SlimResponse ScrapePage(string url)
        {
            string content = string.Empty;
            this.request = (HttpWebRequest)WebRequest.Create(url);
            ((HttpWebRequest)this.request).UserAgent = "NCSA_Mosaic/2.0 (Windows 3.1)";
            this.request.Method = "GET";
            SlimResponse rsp = new SlimResponse();

            using (this.response = (HttpWebResponse)this.request.GetResponse())
            {
                rsp.ContentLength = this.response.ContentLength;
                rsp.StatusCode = this.response.StatusCode;
                using (Stream responseStream = this.response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        content = reader.ReadToEnd();
                    }
                }

                rsp.Content = content;
            }

            return rsp;
        }
    }
}
